package com.actions.bluetoothbox2.util;

import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;

import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.widget.ColorPickView;

/**
 * Created by liuyong on 2017/12/25.
 */

public class PopupColorSelecter2 extends PopupWindow implements View.OnClickListener {

    private onSelecteListener selected;
    private SeekBar seekbar_color;
    private ColorPickView color_picker_view;
    private View view_color;
    private ColorPickGradient mColorPickGradient;
    private int s_color;
    private int select_color =  0xFFFFFF;
    private int seekbar_pre = 0;

    public PopupColorSelecter2(Context context) {
        super(context);
        View view = LayoutInflater.from(context).inflate(R.layout.pop_colorselect2_layout, null);

//        GridView pop_grid = (GridView) view.findViewById(R.id.pop_grid);
        color_picker_view = (ColorPickView) view.findViewById(R.id.color_picker_view);
        seekbar_color = (SeekBar) view.findViewById(R.id.seekbar_color);
        view_color = view.findViewById(R.id.view_color);

        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btn_ok = (Button) view.findViewById(R.id.btn_ok);

        btn_cancel.setOnClickListener(this);
        btn_ok.setOnClickListener(this);
        color_picker_view.setOnColorChangedListener(new ColorPickView.OnColorChangedListener() {
            @Override
            public void onColorChange(int color) {
//                select_color = color;
                Log.i("TAG","color====" + color);
                select_color = color;
                setSeekbarBg(new int[]{color, 0xFF000000});
                float radio = (float) seekbar_pre / seekbar_color.getMax();
                int mColor = mColorPickGradient.getColor(radio);
                view_color.setBackgroundColor(mColor);
                s_color = mColor;
            }
        });
        seekbar_color.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                seekbar_pre = progress;
                float radio = (float) seekbar_pre / seekBar.getMax();
                if(null == mColorPickGradient){
                    setSeekbarBg(new int[]{select_color, 0xFF000000});
                }
                int mColor = mColorPickGradient.getColor(radio);
                view_color.setBackgroundColor(mColor);
                s_color = mColor;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        setContentView(view);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    public void setSelected(onSelecteListener selected) {
        this.selected = selected;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_ok:
                this.dismiss();
                if (null != selected) {
                    selected.onSelected(s_color);
                }
                break;
            default:
                break;
        }
    }

    public interface onSelecteListener {
        void onSelected(int color);
    }

    private void setSeekbarBg(final int[] mColorArr) {
        mColorPickGradient = new ColorPickGradient(mColorArr);
        ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
            @Override
            public Shader resize(int width, int height) {
                LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
                        mColorArr, ColorPickGradient.PICKCOLORBAR_POSITIONS, Shader.TileMode.REPEAT);
                return linearGradient;
            }
        };
        PaintDrawable paint = new PaintDrawable();
        RectShape mRectShape = new RectShape();
        paint.setShape(mRectShape);
        paint.setCornerRadius(5);
        paint.setShaderFactory(shaderFactory);
        seekbar_color.setProgressDrawable(paint);
    }
}
